import 'package:flustars_flutter3/flustars_flutter3.dart';
import 'package:flutter/material.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:flutter_taotaoshipin/http/dao/userinfo_dao.dart';
import 'package:flutter_taotaoshipin/pages/me/me_routes.dart';
import 'package:flutter_taotaoshipin/widget/load_image.dart';
import 'package:get/get.dart';

import 'report_result_page.dart';

class ReportPage extends StatefulWidget {
  const ReportPage({super.key});

  @override
  State<ReportPage> createState() => _ReportPageState();
}

class _ReportPageState extends State<ReportPage> {
  List<ReportEntity> reportList = [
    ReportEntity(title: '我不喜欢'),
    ReportEntity(title: '侵犯权益', subTitle: '搬运抄袭，或侵犯肖像、隐私、名誉、商标、专利权'),
    ReportEntity(title: '色情低俗'),
    ReportEntity(title: '违法犯罪'),
    ReportEntity(title: '政治敏感'),
    ReportEntity(title: '违规营销'),
    ReportEntity(title: '不实信息'),
    ReportEntity(title: '危害人身安全'),
    ReportEntity(title: '未成年相关'),
    ReportEntity(title: 'AI生成内容问题'),
    ReportEntity(title: '以上没有我想举报的类型'),
  ];
  int? currentIndex;

  @override
  void initState() {
    super.initState();
    // 上报
    ddskm_trackReport(UserinfoBehaviorTipOffEvent.open);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        automaticallyImplyLeading: false,
        backgroundColor: Colors.white,
        title: Text('举报'),
        actions: [
          IconButton(
            onPressed: () {
              // 上报
              ddskm_trackReport(UserinfoBehaviorTipOffEvent.close);

              Get.back();
            },
            icon: Icon(
              Icons.close,
              color: Color(0xFFBCBCBC),
            ),
          ),
        ],
      ),
      backgroundColor: Colors.white,
      body: ListView.builder(
        itemCount: reportList.length,
        itemBuilder: (_, index) {
          var model = reportList[index];
          return ListTile(
            title: Text(model.title),
            titleTextStyle: TextStyle(color: Color(0xFF2D2D2D), fontSize: 14),
            subtitle: model.subTitle != null ? Text(model.subTitle!) : null,
            subtitleTextStyle:
                TextStyle(color: Color(0xFFBCBCBC), fontSize: 12),
            trailing: LoadAssetImage(
              currentIndex == index
                  ? 'report/report_radio_sel'
                  : 'report/report_radio',
              width: 20,
            ),
            onTap: () {
              setState(() {
                currentIndex = index;
              });
            },
          );
        },
      ),
      bottomNavigationBar: Container(
        padding: EdgeInsets.fromLTRB(31, 13, 31, 13),
        child: SafeArea(
          child: GestureDetector(
            onTap: () {
              reportBtnAction();
            },
            child: Container(
              width: double.infinity,
              height: 41,
              alignment: Alignment.center,
              decoration: BoxDecoration(
                color: Color(0xFFEB435A),
                borderRadius: BorderRadius.circular(8),
              ),
              child: Text(
                '下一步',
                style: TextStyle(
                  color: Colors.white,
                  fontSize: 14,
                ),
              ),
            ),
          ),
        ),
      ),
    );
  }

  void reportBtnAction() {
    if (currentIndex == null) {
      EasyLoading.showToast('请选择一项');
    } else if (currentIndex == reportList.length - 1) {
      // 上报
      ddskm_trackReport(UserinfoBehaviorTipOffEvent.submit);

      Get.back();
      Get.toNamed(MeRoutes.feedback);
    } else {
      // 上报
      ddskm_trackReport(UserinfoBehaviorTipOffEvent.submit);

      EasyLoading.show(status: '正在提交');
      Future.delayed(
        Duration(seconds: 2),
        () {
          EasyLoading.dismiss();
          Get.back();
          Get.dialog(
            const ReportResultPage(),
            barrierDismissible: false,
            useSafeArea: false,
          );
        },
      );
    }
  }

  Future<void> ddskm_trackReport(
      UserinfoBehaviorTipOffEvent event) async {
    try {
      var result = await UserinfoDao.trackReport(
        event: event,
        eventType: currentIndex != null ? currentIndex! + 1 : 0,
      );
      if (result.code == 0) {
        if (result.data?.state == true) {
          LogUtil.e('举报事件上报成功');
        } else {
          LogUtil.e('举报事件上报失败');
        }
      } else {
        LogUtil.e('举报事件上报失败');
      }
    } catch (e) {
      LogUtil.e(e);
    }
  }
}

class ReportEntity {
  final String title;
  final String? subTitle;

  ReportEntity({
    required this.title,
    this.subTitle,
  });
}
